package com.yikaiye.android.yikaiye.ui.activity.order;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityChooseCompanyBinding;
import com.yikaiye.android.yikaiye.ui.activity.order.viewmodel.MyCompanyViewModel;
import com.yikaiye.android.yikaiye.ui.adapter.ChooseCompanyAdapter;
import com.yikaiye.android.yikaiye.utils.CommonUtil;

/**
 * 选择企业
 */
public class ChooseCompanyActivity extends BaseActivity<ActivityChooseCompanyBinding, MyCompanyViewModel> {

    private ChooseCompanyAdapter adapter;
    private int page = 1;
    private String keyword;

    @Override
    protected MyCompanyViewModel bindModel() {
        return getViewModel(this, MyCompanyViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.choose_company));
        adapter = new ChooseCompanyAdapter(context);
        binding.recyclerView.setAdapter(adapter);
        adapter.setEmptyViewEnable(true);
        adapter.setEmptyViewLayout(this, R.layout.layout_empty_list);
        viewModel.getMyCompanyList(page, keyword);
    }

    @Override
    protected void initClick() {
        viewModel.companyRecordsModelMutableLiveData.observe(this, companyRecordsModel -> {
            if (companyRecordsModel != null) {
                if (page == 1) {
                    adapter.submitList(companyRecordsModel.getRecords());
                    binding.smartRefreshLayout.finishRefresh();
                } else {
                    adapter.addAll(companyRecordsModel.getRecords());
                    binding.smartRefreshLayout.finishLoadMore();
                }
            }
        });

        viewModel.mErrorData.observe(this, s -> {
            binding.smartRefreshLayout.finishRefresh();
            binding.smartRefreshLayout.finishLoadMore();
        });

        binding.etSearchContent.setOnEditorActionListener((textView, i, keyEvent) -> {
            CommonUtil.closeKeyboard(this);
            keyword = binding.etSearchContent.getText().toString();
            page = 1;
            binding.recyclerView.scrollToPosition(0);
            viewModel.getMyCompanyList(page, keyword);
            return true;
        });

        binding.etSearchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.clearIv.setVisibility(charSequence.toString().isEmpty() ? View.GONE : View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        viewModel.onDelayClick(binding.clearIv, o -> {
            keyword = null;
            binding.etSearchContent.setText("");
        });

        adapter.setOnChildItemClickListener(companyModel -> {
            Intent intent = new Intent();
            intent.putExtra(Constant.MODEL_KEY, companyModel);
            setResult(RESULT_OK, intent);
            finish();
        });

        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                viewModel.getMyCompanyList(page, keyword);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                viewModel.getMyCompanyList(page, keyword);
            }
        });
    }
}